#include "pdfkit.h"
#include <QDebug>

PDFKit::PDFKit(QString path):multiple(1), fileName(path)
{
    document = Document::load(path);
    if(document->isLocked() || document==NULL)
    {
        qDebug() << "load fail";
        return ;
    }
    document->setRenderHint(Document::Antialiasing);
    document->setRenderHint(Document::TextAntialiasing);

    totalPage = document->numPages();
    pageSize = document->page(0)->pageSize();
    qDebug() << "width = " << pageSize.width();
}

PDFKit::~PDFKit()
{

}

QImage PDFKit::get_actual_image(int pageNum)
{
    QImage image;
    Page *page = document->page(pageNum);
    if(page == NULL)
    {
        qDebug() << "get page fail";
        return image;
    }
    QSize size = get_page_size(pageNum);
    image = page->renderToImage(72.0, 72.0, 0, pageNum, size.width(), size.height());
    if(image.isNull())
    {
        qDebug() << "render fail";
        return image;
    }
    delete page;
    return image;
}


QImage PDFKit::get_page_image(int pageNum)
{
    QImage image;
    Page *page = document->page(pageNum);
    if(page == NULL)
    {
        qDebug() << "get page fail";
        return image;
    }
    QSize size = get_page_size(pageNum);
    image = page->renderToImage(72.0*multiple, 72.0*multiple, 0, pageNum, size.width()*multiple, size.height()*multiple);
    if(image.isNull())
    {
        qDebug() << "render fail";
        return image;
    }
    delete page;
    return image;
}

QSize PDFKit::get_page_size(int p)
{
    Page *page = document->page(p);
    return page->pageSize();
}

QImage PDFKit::GetActruallyPageImage(int pageNum)
{
    Page *page = document->page(pageNum);
    QSize size = get_page_size(pageNum);
    QImage image = page->renderToImage(72.0, 72.0, 0, pageNum, size.width(), size.height());
    return image;
}


